package com.youshang.designpattern;

import com.google.common.collect.Lists;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class q1p {
    public static void main(String[] args) throws InterruptedException {
        int m = Integer.parseInt("16"); // 网格大小
        int n = Integer.parseInt("10"); // 生成点数量
        int s = Integer.parseInt("1"); // 停顿时间
        int t = Integer.parseInt("5"); // 最大线程数

        List<CharacterP> characterPS = new ArrayList<>(); // 角色列表

        // 在地图外围生成角色
        Random random = new Random();
        for (int i = 0; i < n; i++) {
            int x = random.nextInt(2 * m) - m;
            int y = random.nextInt(2 * m) - m;
            characterPS.add(new CharacterP(x, y, m));
        }

        long startTime = System.currentTimeMillis();

        // 使用线程池执行角色的移动任务
        ExecutorService executorService = Executors.newFixedThreadPool(t);
        for (CharacterP characterP : characterPS) {
            executorService.execute(() -> characterP.move(characterPS));
        }

        // 等待模拟运行结束
        executorService.shutdown();
        executorService.awaitTermination(10, TimeUnit.SECONDS);

        int totalMoves = 0;
        for (CharacterP characterP : characterPS) {
            totalMoves += characterP.getTotalMoves();
        }

        System.out.println("Total moves: " + totalMoves);
    }
}

class CharacterP implements Runnable {
    private int x;
    private int y;
    private int m;
    private boolean alive;
    private int totalMoves;

    public CharacterP(int x, int y, int m) {
        this.x = x;
        this.y = y;
        this.m = m;
        this.alive = true;
        this.totalMoves = 0;
    }

    public boolean isAlive() {
        return alive;
    }

    public void move(List<CharacterP> characterPS) {
        Random random = new Random();
        int targetX = random.nextInt(2 * m) - m;
        int targetY = random.nextInt(2 * m) - m;

        // 使用 Bresenham 算法计算直线路径
        List<int[]> path = calculatePath(x, y, targetX, targetY);

        // 执行移动
        for (int[] step : path) {
            int nextX = step[0];
            int nextY = step[1];

            // 检查是否与其他角色重叠
            boolean overlap = false;
            for (CharacterP other : characterPS) {
                if (other != this && other.isAlive() && other.getX() == nextX && other.getY() == nextY) {
                    overlap = true;
                    break;
                }
            }

            if (overlap) {
                alive = false;
                break;
            }

            x = nextX;
            y = nextY;

            // 到达目的地
            if (x == targetX && y == targetY) {
                break;
            }
        }

        totalMoves += path.size();
    }

    private List<int[]> calculatePath(int startX, int startY, int targetX, int targetY) {
        // 使用 Bresenham 算法计算直线路径
        List<int[]> path = new ArrayList<>();
        int dx = Math.abs(targetX - startX);
        int dy = Math.abs(targetY - startY);
        int sx = startX < targetX ? 1 : -1;
        int sy = startY < targetY ? 1 : -1;
        int err = dx - dy;

        while (true) {
            path.add(new int[]{startX, startY});

            if (startX == targetX && startY == targetY) {
                break;
            }

            int e2 = 2 * err;
            if (e2 > -dy) {
                err -= dy;
                startX += sx;
            }
            if (e2 < dx) {
                err += dx;
                startY += sy;
            }
        }

        return path;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public int getTotalMoves() {
        return totalMoves;
    }

    @Override
    public void run() {
        move(Lists.newArrayList());
    }
}
